﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraEditors;

using DevTeamIT;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.TaskManager
{
    public partial class Tasks : XForm
    {
        public Tasks()
        {
            InitializeComponent();
        }

        DataTable dtTasks;
        private void Tasks_Load(object sender, EventArgs e)
        {
            ShowOnlyFooter();

            dtTasks = Session.DataManager.ExecuteDataTable("TM_GetPendingTasks", WhoAmI);

            if (dtTasks.Rows.Count == 0)
            {
                ShowMessage("Bekleyen göreviniz yok. Teşekkür ederiz.");
                return;
                //this.Close();
            }


            foreach (DataRow dr in dtTasks.Rows)
            {
                Panel panel = new Panel();
                panel.Size = tPanel.Size;
                panel.BackColor = Color.FromName(dr["Color"].ToString());

                LabelControl lblHeader = new LabelControl();
                panel.Controls.Add(lblHeader);
                lblHeader.Text = dr["Header"].ToString();
                lblHeader.AutoSizeMode = tlblHeader.AutoSizeMode;
                lblHeader.Size = tlblHeader.Size;
                lblHeader.Font = tlblHeader.Font;
                lblHeader.Appearance.TextOptions.HAlignment = tlblHeader.Appearance.TextOptions.HAlignment;
                lblHeader.Appearance.TextOptions.VAlignment = tlblHeader.Appearance.TextOptions.VAlignment;
                lblHeader.Appearance.TextOptions.WordWrap = tlblHeader.Appearance.TextOptions.WordWrap;
                lblHeader.Location = tlblHeader.Location;
                lblHeader.BorderStyle = tlblHeader.BorderStyle;
                lblHeader.Click += TaskPanel_Click;
                //lblHeader.MouseHover += panel_MouseHover;
                //lblHeader.MouseLeave += panel_MouseLeave;

                LabelControl lblDescription = new LabelControl();
                panel.Controls.Add(lblDescription);
                lblDescription.Text = dr["Description"].ToString();
                lblDescription.AutoSizeMode = tlblDescription.AutoSizeMode;
                lblDescription.Size = tlblDescription.Size;
                lblDescription.Font = tlblDescription.Font;
                lblDescription.Appearance.TextOptions.HAlignment = tlblDescription.Appearance.TextOptions.HAlignment;
                lblDescription.Appearance.TextOptions.VAlignment = tlblDescription.Appearance.TextOptions.VAlignment;
                lblDescription.Appearance.TextOptions.WordWrap = tlblDescription.Appearance.TextOptions.WordWrap;
                lblDescription.Location = tlblDescription.Location;
                lblDescription.BorderStyle = tlblDescription.BorderStyle;
                lblDescription.Padding = tlblDescription.Padding;
                lblDescription.Click += TaskPanel_Click;
                //lblDescription.MouseHover += panel_MouseHover;
                //lblDescription.MouseLeave += panel_MouseLeave;

                LabelControl lblAdet = new LabelControl();
                panel.Controls.Add(lblAdet);
                lblAdet.Text = ((int)dr["Counts"]).ToString("n0");
                lblAdet.AutoSizeMode = tlblAdet.AutoSizeMode;
                lblAdet.Size = tlblAdet.Size;
                lblAdet.Font = tlblAdet.Font;
                lblAdet.Appearance.TextOptions.HAlignment = tlblAdet.Appearance.TextOptions.HAlignment;
                lblAdet.Appearance.TextOptions.VAlignment = tlblAdet.Appearance.TextOptions.VAlignment;
                lblAdet.Appearance.TextOptions.WordWrap = tlblAdet.Appearance.TextOptions.WordWrap;
                lblAdet.Location = tlblAdet.Location;
                lblAdet.BorderStyle = tlblAdet.BorderStyle;
                lblAdet.Click += TaskPanel_Click;
                //lblAdet.MouseHover += panel_MouseHover;
                //lblAdet.MouseLeave += panel_MouseLeave;

                Label lblCizgi = new Label();
                panel.Controls.Add(lblCizgi);
                lblCizgi.Size = new System.Drawing.Size(lblDescription.Width, 1);
                lblCizgi.Location = tlblDescription.Location;
                lblCizgi.BorderStyle = BorderStyle.FixedSingle;
                lblCizgi.BringToFront();


                panel.Cursor = Cursors.Hand;
                panel.Tag = (int)dr["UserTaskId"];
                panel.MouseClick += TaskPanel_Click;
                // panel.MouseEnter += panel_MouseHover;
                // panel.MouseLeave += panel_MouseLeave;

                Panels.Controls.Add(panel);

                lblAdet.SendToBack();
                lblCizgi.SendToBack();
                lblDescription.SendToBack();
                panel.BringToFront();
            }
            Panels.Padding = new System.Windows.Forms.Padding(10);
            //  tPanel.Visible = false;
        }

        //void panel_MouseLeave(object sender, EventArgs e)
        //{

        //    this.Text = "leave";
        //    return;

        //    //Control senderControl = (Control)sender;
        //    Panel senderPanel = (Panel)sender;
        //    //if (senderControl.Parent.GetType() == typeof(Panel))
        //    //    senderPanel = (Panel)senderControl.Parent;
        //    //else            
        //    if (!senderPanel.Bounds.Contains(this.PointToClient(Control.MousePosition)))
        //    {
        //        this.Text = "leave";
        //        senderPanel.BackColor = latestPanelColor;
        //    }

        //}

        //Color latestPanelColor;
        //void panel_MouseHover(object sender, EventArgs e)
        //{
        //    Panel senderPanel = (Panel)sender;
        //    this.Text = "hover";
        //    return;

        //    if (senderPanel.Bounds.Contains(this.PointToClient(Control.MousePosition)))
        //    {
        //        this.Text = "hover";
        //        //Control senderControl = (Control)sender;                
        //        //if (senderControl.Parent.GetType() == typeof(Panel))
        //        //    senderPanel = (Panel)senderControl.Parent;
        //        //else

        //        latestPanelColor = senderPanel.BackColor;
        //        double carpan = 0.8;
        //        senderPanel.BackColor = Color.FromArgb(latestPanelColor.A, Convert.ToInt16(latestPanelColor.R * carpan), Convert.ToInt16(latestPanelColor.G * carpan), Convert.ToInt16(latestPanelColor.B * carpan));
        //    }
        //}


        private void TaskPanel_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            Control senderControl = (Control)sender;
            int TaskId;
            if (senderControl.Parent.GetType() == typeof(Panel))
                TaskId = (int)senderControl.Parent.Tag;
            else
                TaskId = (int)senderControl.Tag;

            DataRow dr = dtTasks.Select("UserTaskId=" + TaskId)[0];

            switch (TaskId)
            {
                case 1:
                case 2:
                    AppSession.MainForm.ShowMdiChildForm(typeof(SingleGridTask), dr["Header"], dr["SP"]);
                    break;
                
            }
            this.Cursor = Cursors.Default;
        }

        private void Panels_MouseMove(object sender, MouseEventArgs e)
        {
            this.Text = (int.Parse(this.Text) + 1).ToString();
        }
    }
}